package com.link_with_health.common;

import com.link_with_health.exception.BaseException;

import java.util.List;
import java.util.ArrayList;

public class Page<T> {
    public List<T> data;
    public Integer pageSize;
    public Integer currentPage;

    public Page(List<T> data, Integer pageSize, Integer currentPage){
        this.data = data;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    public List<T> currentPageData() throws Exception{
        ArrayList<T> content = new ArrayList<T>(this.pageSize);

        int index = (this.currentPage-1) * this.pageSize;

        if(index > data.size())
            throw new BaseException("[异常页数！！]");

        for(int i = 0; i < currentContentNum(); i++)
            content.add(data.get(index+i));

        return content;
    }

    private Integer currentContentNum(){
        return isLastPage() ? this.data.size() % this.pageSize
                            : this.pageSize;
    }

    /**
     * 查看是否为最后页
     * @return 判断
     */
    private Boolean isLastPage(){
        return this.currentPage * this.pageSize > this.data.size();
    }


    public PageData<T> getPageDate(){

        try {
            return new PageData(currentPageData(),currentPage,pageSize,data.size());
        } catch (Exception e) {
            System.out.println("[异常页数]");
        }
        return new PageData<>(null,currentPage,pageSize,0);
    }


    public class PageData<T>{
        public List<T> data;
        public int currentPage;
        public int size;
        public int total;

        public PageData() {
        }

        public PageData(List<T> data, int currentPage, int size, int total) {
            this.data = data;
            this.currentPage = currentPage;
            this.size = size;
            this.total = total;
        }

        public List<T> getData() {
            return data;
        }

        public void setData(List<T> data) {
            this.data = data;
        }

        public int getCurrentPage() {
            return currentPage;
        }

        public void setCurrentPage(int currentPage) {
            this.currentPage = currentPage;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public int getTotal() {
            return total;
        }

        public void setTotal(int total) {
            this.total = total;
        }
    }
}
